/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;

import com.taobao.snowsnow.hibernate.dao.ExpressRecordDao;
import com.taobao.snowsnow.hibernate.pojo.ExpressRecord;
import com.taobao.snowsnow.hibernate.pojo.Trade;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ExpressRecordDaoImpl extends BaseDaoImpl implements ExpressRecordDao {
	
	public ExpressRecord findById(int id) throws Exception {
		return (ExpressRecord) getHibernateTemplate().get(ExpressRecord.class, id);
	}
	
	public List<ExpressRecord> findByName(String name) throws Exception {
		return super.findByProperty(ExpressRecord.class, "name", name);
	}

	public List<ExpressRecord> findAll() throws Exception {
		return getHibernateTemplate().find("from ExpressRecord");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(ExpressRecord.class);
	}

	public List<ExpressRecord> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(ExpressRecord.class, page, limit);
	}

	@Override
	public ExpressRecord getFirstObject() throws Exception {
		Query query = getSession().createQuery("from ExpressRecord as o where o.id = (select min(id) from ExpressRecord)");
		List<ExpressRecord> list = query.list();
		if(list.size() !=0)
			return (ExpressRecord) list.get(0);
		else return null;
	}

	@Override
	public ExpressRecord getLastObject() throws Exception {
		Query query = getSession().createQuery("from ExpressRecord as o where o.id = (select max(id) from ExpressRecord)");
		List<ExpressRecord> list = query.list();
		if(list.size() !=0)
			return (ExpressRecord) list.get(0);
		else return null;
	}

	@Override
	public ExpressRecord findByNamePrecise(String name) throws Exception {
		List<ExpressRecord> list = findByName(name);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public List<ExpressRecord> findByCriteria(List<Criterion> expression)
			throws Exception {
		Criteria c = getSession().createCriteria(ExpressRecord.class);
		for(Criterion tmp : expression) {
			c.add(tmp);
		}
		return c.list();
	}
}
